//
//  UIControl+Monitor.m
//  JKRuntimeMethod_exchange
//
//  Created by 王冲 on 2018/10/10.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "UIControl+Monitor.h"
#import <objc/runtime.h>
@implementation UIControl (Monitor)

+(void)load{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Method method1 = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
        Method method2 = class_getInstanceMethod(self,@selector(jk_sendAction:to:forEvent:));
        method_exchangeImplementations(method1, method2);
        
    });
}

-(void)jk_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    
    NSLog(@"%@-%@-%@",self,target,NSStringFromSelector(action));
    
    // 调用系统原来的实现(解释：这里看似是调用自己的死循环，其实正是利用了方法的交换来实现原来的方法)
    [self jk_sendAction:action to:target forEvent:event];
    
    if ([self isKindOfClass:[UIButton class]]) {
        
        // 拦截了所有按钮的事件
    }
}

@end
